import { _decorator, Component, director, find, instantiate, JsonAsset, Label, Node, Prefab, resources, sys, tween, v3 } from 'cc';
import { Dictionary } from '../utils/Dictionary';
import { UserInfo } from './UserInfo';
import { Role } from './Role';
import { Equip } from './Equip';
import { roleEquipEnum } from '../Enum';
import { StorageMgr } from '../StorageMgr';
import { Pet } from './Pet';
const { ccclass, property } = _decorator;

@ccclass('StartSceneGameMgr')
export class StartSceneGameMgr extends Component {
     

    static ins:StartSceneGameMgr;
    sdk = window['wx']||window['tt']
    seeAdvGetCrystalTotalNum=2;
    seeAdvGetCoinTotalNum=1;
    freeBoxOpenTotalNum=5;
    drawTurntableTotalNum=5;

    skillPrefabDic:Dictionary<string,Prefab>=new Dictionary();//技能预制体字典

    roleSelectId;//选择已拥有角色的item，值为角色id，选择角色碎片item，值为角色类型负的值
    sceneGateLevel=1;//当前游戏场景的关卡数

    _windowWidth=750;
    _windowHeight=1334;

    enemyGenerateY=660;
    attackEnemyNeedDistance=1400;//敌人到达离角色多少距离时，才开始攻击
    conversionWindowPxY;//设备长和游戏内像素的换算;
    isAudioBgm=true;

    private equipId=0;//递增，装备唯一标识
    private petId=0;//递增，宠物唯一标识
    private roleId=0;//递增，角色唯一标识

    set EquipId(value){
        this.equipId=value;
        StorageMgr.ins.setStorage('equipId',this.equipId);
    }
    get EquipId() {
        return this.equipId;
    }

    set PetId(value){
        this.petId=value;
        StorageMgr.ins.setStorage('petId',this.petId);
    }
    get PetId() {
        return this.petId;
    }

    set RoleId(value){
        this.roleId=value;
        StorageMgr.ins.setStorage('roleId',this.roleId);
    }
    get RoleId() {
        return this.roleId;
    }

    protected onLoad(): void {
        StartSceneGameMgr.ins=this;
    }

    protected start(): void {
        if (this.sdk) {
            this._windowWidth = this.sdk.getSystemInfoSync().windowWidth;
            this._windowHeight = this.sdk.getSystemInfoSync().windowHeight;

            this.attackEnemyNeedDistance = this._windowHeight / 4.5 * this.conversionWindowPxY;
            console.log("this.attackEnemyNeedDistance99",this.attackEnemyNeedDistance)
            this.conversionWindowPxY = 1334 / this._windowHeight;
            this.enemyGenerateY=660+(StartSceneGameMgr.ins._windowHeight/2-333)*StartSceneGameMgr.ins.conversionWindowPxY;
        }
      
        console.log("设备长宽",this.sdk.getSystemInfoSync(),this._windowWidth,this._windowHeight)

    }


    //获得新角色
    getRole(_roleType){
            let roleClass=new Role();
            roleClass.initData({
                roleId:this.RoleId,
                roleType:_roleType
            })
            UserInfo.hasRoleDic.set(roleClass.roleId,roleClass);
            UserInfo.accumulateHasRoleNum+=1;
            StartSceneGameMgr.ins.saveGameDate();
            this.RoleId+=1;
    }

    //获得新装备
    getEquip(_equipType){
        let equipNewClass=new Equip();
        equipNewClass.initData({
            equipType:_equipType,
            enhanceLevel:0,
            equipId:this.equipId
        })
        UserInfo.hasEquipDic.set(equipNewClass.equipId,equipNewClass);
        StorageMgr.ins.setStorage('equipData',UserInfo.hasEquipDic.getValueList());
        this.EquipId+=1;
    }

    //获得新宠物
    getPet(_petType) {
        let petClass=new Pet();
        petClass.initData({
            petType:_petType,
            petLevel:1,
            petId:this.petId
        })
        UserInfo.hasPetDic.set(petClass.petId,petClass);
        StorageMgr.ins.setStorage('petData',UserInfo.hasPetDic.getValueList());
        this.PetId+=1;
    }


    saveGameDate(){
        let userInfoData = {
            level: UserInfo.level,
            crystalMun:UserInfo.crystalMun,
            coinMun:UserInfo.coinMun,
            currGoBattleRoleId:UserInfo.currGoBattleRoleId,
            seeAdvGetCrystalUseNum:UserInfo.seeAdvGetCrystalUseNum,
            seeAdvGetCoinUseNum:UserInfo.seeAdvGetCoinUseNum,
            todayAlreadyIsSign:UserInfo.todayAlreadyIsSign,
            openFreeBoxNum:UserInfo.openFreeBoxNum,
            drawTurnTableNum:UserInfo.drawTurnTableNum,
            signContinuousDayNum:UserInfo.signContinuousDayNum,
            todayEnhanceEquipNum:UserInfo.todayEnhanceEquipNum,
            todayBeatEnemyNum:UserInfo.todayBeatEnemyNum,
            todayOpenBoxNum:UserInfo.todayOpenBoxNum,
            todayWinGateNum:UserInfo.todayWinGateNum,
            todayEveryDayTask:UserInfo.todayEveryDayTask,
            epicBoxKeyNum:UserInfo.epicBoxKeyNum,
            legendBoxKeyNum:UserInfo.legendBoxKeyNum,
            hasEndSkillPropArr:UserInfo.hasEndSkillPropArr,
            receiveAchievementArr:UserInfo.receiveAchievementArr,
            accumulateEvolvePlayerSkillFireBall:UserInfo.accumulateEvolvePlayerSkillFireBall,
            accumulateEvolvePlayerSkillSpike:UserInfo.accumulateEvolvePlayerSkillSpike,
            accumulateEvolvePlayerSkillStarLightBullet:UserInfo.accumulateEvolvePlayerSkillStarLightBullet,
            accumulateHasRoleNum:UserInfo.accumulateHasRoleNum,
        }
        StorageMgr.ins.setStorage('userInfo',userInfoData);
        StorageMgr.ins.setStorage('roleData',UserInfo.hasRoleDic.getValueList());
        StorageMgr.ins.setStorage('roleChipData',UserInfo.hasRoleChipArr);
        StorageMgr.ins.setStorage('equipData',UserInfo.hasEquipDic.getValueList());
        StorageMgr.ins.setStorage('petData',UserInfo.hasPetDic.getValueList());
        StorageMgr.ins.setStorage('petChipData',UserInfo.hasPetChipArr);

    }

    createWidowTip(str) {
        resources.load("prefabs/ele/widowPopTipPre2", Prefab, (err, res) => {
            let pre = instantiate(res);
            pre.setParent(find("Canvas/windowTipRoot"));
            pre.setPosition(0, -50);
            pre.getChildByName("Label").getComponent(Label).string = str;
            tween(pre)
                .to(0.5, { position: v3(0, 0, 0) })
                .delay(0.7)
                .call(() => {
                    pre.destroy();
                })
                .start()
        })

    }

    //获得上阵宠物
    getGoBattlePet(){
        let goBattlePet = null;
        if (UserInfo.hasPetDic.lenght>0) {
            UserInfo.hasPetDic.getValueList().forEach((ele) => {
                if (ele.isGoBattle) {
                    goBattlePet=ele
                }
            })
        }
      
        return goBattlePet;
    }

}


